'''
Created on 2010/08/11

@author: yukiyonezawa
'''
from pyrfid.asn1.Encoder import Encoder

class IDTable:
    
    def __init__(self): 
        self.kText = ''
        self.kVersion = ''
        self.kTableID = ''
        self.kRootOID = ''
        self.kIDSize = ''
        self.kTableEnd = ''
        self.idInfos = []
        # define every ID Table as a dictionary with the key of ID Value
        # define the IDTable as a dictionary of a dictionary with the key of ID Value
    
    def getKText(self):
        return self.kText
    
    def getKVersion(self):
        return self.kVersion
    
    def getKTableID(self):
        return self.kTableID
    
    def getKRootOID(self):
        return self.kRootOID
    
    def getKIDSize(self):
        return self.kIDSize
    
    def getKTableEnd(self):
        return self.kTableEnd
    
    def getIDInfoBinary(self):
        return self.idInfos                
    
    def getIDInfoByObjectIdentifier(self, object_identifier):        
        for info in self.idInfos:
            for OIDString in info.getOIDString():
                if OIDString == "" or OIDString[0] == "(":
                    continue
                if Encoder.oidEncode(self.getKRootOID() + " " + OIDString) == object_identifier:
                    return info
    
    
    def getIDInfo(self, is_oid_chain=False):
        
        OID_list = list() 
        if is_oid_chain == False:
            for info in self.idInfos:
                if info.getOIDString() == "" or info.getOIDString()[0] == "":
                    continue
                if info.getOIDString()[0][0] != "(":
                    OID_list.append(info)
            return OID_list
        
        for info in self.idInfos:            
            if info.getOIDString() == "" or info.getOIDString()[0] == "":
                continue
            
            if info.getOIDString()[0][0] != "(":
                continue
            
            value = info.getOIDString()[0][1:]
            value = value[:len(value) - 1]
            OID_list.append({"Info":info, "ID":value.split(")(")})            
        
        return OID_list
    
#        for info in self.idInfos:
#            if key == 0:
#                item = info.getIDString()                
#                if '%' in item:
#                    parts = item.split('%x')
#                    start = int(parts[1].split('-')[0].strip(), 16) - 48
#                    end = int(parts[1].split('-')[1].strip().strip(')'), 16) - 48
#                    for i in range(end - start):
#                        if parts[0].strip().strip('(') + str(start + i) == value:
#                            return info
#                elif item == value:
#                    return info                        
#            elif key == 1:
#                item = info.getIDValue()
#                if item == value:
#                    return info
#            else:
#                raise "NotSupportedError"
#        raise "WrongIDError"
    
    def getIDInfoAll(self):
        return self.idInfos        

    def setKText(self, kText):
        self.kText = kText
    
    def setKVersion(self, kVersion):
        self.kVersion = kVersion
    
    def setKTableID(self, kTableID):
        self.kTableID = kTableID
    
    def setKRootOID(self, kRootOID):
        
        self.kRootOID = kRootOID[len("urn:oid:"):].replace(".", " ")
        
    
    def setKIDSize(self, kIDSize):
        self.kIDSize = kIDSize
    
    def setKTableEnd(self, kTableEnd):
        self.kTableEnd = kTableEnd
    
    def setIDInfo(self, info):
        self.idInfos = info
        
        
    @staticmethod
    def useIDTableInfo(defined_packed_objects, add_data_set_list):
        
        #chain
        IDValueList = list()
        IDInfo = defined_packed_objects.getIDInfo(True)
        chain_IDString = list()
        
        for IDInfoList in IDInfo:
            number_of_IDString = len(IDInfoList["ID"])
            count = 0
            for OIDString in IDInfoList["ID"]:            
                for data_set in add_data_set_list:
                    if data_set.OBJECT_IDENTIFIER == Encoder.oidEncode(defined_packed_objects.getKRootOID() + " " + OIDString):
                        count += 1
            if number_of_IDString == count:                    
                IDValueList.append(IDInfoList["Info"].getIDValue())
                chain_IDString.extend(IDInfoList["ID"])
                
        #not chain
        for data_set in add_data_set_list:
            is_consisted = False
            for OIDString in chain_IDString:
                if Encoder.oidEncode(defined_packed_objects.getKRootOID() + " " + OIDString) == data_set.OBJECT_IDENTIFIER:
                    is_consisted = True
            
            if is_consisted == False:
                info = defined_packed_objects.getIDInfoByObjectIdentifier(data_set.OBJECT_IDENTIFIER)
                IDValueList.append(info.getIDValue())
        
        IDInfoList = list()
        for IDValue in IDValueList:
            for IDInfo  in defined_packed_objects.getIDInfoAll():
                if IDInfo.getIDValue() == IDValue:
                    IDInfoList.append(IDInfo)
                    break
        
        return IDInfoList
    